<template>
  <el-container class="home-container">
    <el-header>
      <div class="header_box">
        <img src="https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg">
        <div style="font-size: 20px;color: #fff;margin-left: 20px">小米商城登录后台管理系统</div>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse?'64px':'200px'">
        <!--控制侧边栏展开关闭的按钮-->
        <div class="toggle_box" @click="toggle_bth">|||</div>
        <!--侧边栏菜单区-->
        <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath"
        >

          <!--一级菜单-->
          <!--这边的整个左侧菜单栏  通过双重v-for来进行多重列表的渲染-->
          <!--这里后续拼接一个空字符串是因为   el 组件的index属性只能接收字符串   而服务器的id是数字  所以要隐式转换一下-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!--一级菜单模板区-->
            <template slot="title">
              <!--这里就是菜单图标-->
              <!--[] 的形式 也可以 获取对象中的属性-->
              <i :class="iconsObj[item.id]"></i>
              <!--这里就是菜单标题-->
              <span>{{ item.authName }}</span>
            </template>
            <!--这里加空字符串的理由和上面的一样-->
            <el-menu-item :index="'/home/'+subItem.path" v-for="subItem in item.children" @click="saveNavState('/home/'+subItem.path)">
              <!--二级菜单模板区-->
              <template slot="title">
                <!--这里就是菜单图标-->
                <i class="el-icon-menu"></i>
                <!--这里就是菜单标题-->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      menuList: [],
      iconsObj: {
        '125': 'el-icon-user-solid',
        '103': 'el-icon-s-help',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-claim'
      },
      isCollapse: false,
      //被激活的链接地址
      activePath:''
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$message({
        message: '退出登录成功',
        type: 'warning'
      });
      this.$router.push('/login')
    },
    async getMeuList() {
      const {data: res} = await this.$http.get('menus');
      //这后面的这个$message  是 饿了么 ui  里 的  消息提示组件
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    toggle_bth() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(tokenPath){
      window.sessionStorage.setItem('tokenPath', tokenPath)
      this.activePath=tokenPath
    }
  },
  created() {
    this.getMeuList();
    //这里就是让 页面即使被刷新了   二级菜单的高亮状态依据可以保存
    this.activePath=window.sessionStorage.getItem('tokenPath')
  }
}
</script>

<style scoped lang="less">
.home-container {
  height: 100%;

}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 80px !important;

  img {
    width: 60px;
    height: 60px;
  }

  .header_box {
    width: 200px;
    height: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }

  .toggle_box {
    background-color: #4A5064;
    color: white;
    text-align: center;
    font-size: 10px;
    line-height: 24px;
    /*letter-spacing 属性增加或减少字符间的空白（字符间距）。*/
    letter-spacing: 0.5em;
  }
}

.el-main {
  background-color: #EAEDF1;
}

</style>
